package com.haoze.nurse;

import com.alibaba.druid.support.http.StatViewServlet;
import com.haoze.admin.CloudAdminApplication;
import com.haoze.nurse.config.MinioConfig;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Executor;

@EnableFeignClients(basePackages = {"com.haoze"})
@EnableCaching
@EnableScheduling
@SpringBootApplication(scanBasePackages = {"com.haoze.nurse","com.haoze.common"})
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan(basePackages = "com.haoze.nurse.dao")
public class CloudNurseApplication {
    private final Logger logger = LoggerFactory.getLogger(CloudNurseApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(CloudNurseApplication.class, args);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public ServletRegistrationBean statViewServlet() {
        return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    }

    @Bean("taskExecutor")
    public Executor taskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(500);
        taskExecutor.setKeepAliveSeconds(60);
        taskExecutor.setThreadNamePrefix("taskExecutor--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        return taskExecutor;
    }

    @Resource
    private MinioConfig minioConfig;

    @Bean
    public MinioClient minioClient() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
        MinioClient minioClient = MinioClient.builder()
                .endpoint(new URL(minioConfig.getMinioUrl()))
                .credentials(minioConfig.getMinioAccessKey(), minioConfig.getMinioSecretKey())
                .build();
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioConfig.getMinioBucket()).build());
        if (!found) {
            // Make a new bucket.
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioConfig.getMinioBucket()).build());
        } else {
            logger.info("Bucket '{}' already exists.", minioConfig.getMinioBucket());
        }
        return minioClient;
    }

}
